//
//  LZListOrHistroyViewController.m
//  沙哈拉FM
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 S1mpl3. All rights reserved.
//

#import "LZListOrHistroyViewController.h"
#import "LZMusicSingleton.h"
#import "FitHeader.h"
#import "LZHistoryTableViewCell.h"
#import "ZBBPlayHistory.h"
#import "LZBillboardModel.h"
@interface LZListOrHistroyViewController ()<UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate>
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, assign)NSInteger flag;
@property(nonatomic, strong)NSMutableArray *sortArray;
@property(nonatomic, strong)UIButton *clearHistoryButton;
@end

@implementation LZListOrHistroyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = _titleString;
    self.view.backgroundColor = [UIColor orangeColor];
    self.navigationController.navigationBarHidden = NO;
    _flag = 0;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"降序" style:UIBarButtonItemStylePlain target:self action:@selector(sortDescending)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor redColor];
}
- (void)loadView{
    [super loadView];
    _sortArray = [NSMutableArray array];
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
    
    _clearHistoryButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _clearHistoryButton.frame = CGRectMake(0, 0, self.view.frame.size.width, 40 * FITHeight);
    [_clearHistoryButton setTitle:@"清空历史" forState:UIControlStateNormal];
    [_clearHistoryButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    _clearHistoryButton.titleLabel.font = [UIFont systemFontOfSize:16];
    _clearHistoryButton.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
    [_clearHistoryButton addTarget:self action:@selector(clearHistoryAction) forControlEvents:UIControlEventTouchUpInside];
    
    [self getData];
}
#pragma mark - 降序
- (void)sortDescending{
    if (_flag == 0) {
        self.navigationItem.rightBarButtonItem.title = @"升序";
        _dataArray = [[_dataArray reverseObjectEnumerator] allObjects];
        [_tableView reloadData];
        _flag = 1;
    }else{
        self.navigationItem.rightBarButtonItem.title = @"降序";
        _dataArray = [[_dataArray reverseObjectEnumerator] allObjects];
        [_tableView reloadData];
        _flag = 0;
    }
    
}
#pragma mark - 清空历史
- (void)clearHistoryAction{
    UIAlertView *alterView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"确定清空播放历史" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alterView show];
}
#pragma mark - alterView点击方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        [[ZBBPlayHistory shareInstance] removeHistory:@"LZString"];
        
        [_sortArray removeAllObjects];
        
        [_tableView reloadData];
    }
}
#pragma mark - cell的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (_index == 100) {
        return _dataArray.count;
    }
    return _sortArray.count;
}
- (void)getData{
    _sortArray = [[ZBBPlayHistory shareInstance] takeHistoryArrayFromFile:@"LZString"];
    [_tableView reloadData];
}
#pragma mark - cell的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_index == 100) {
        static NSString *identifier = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        cell.textLabel.text = [_dataArray[indexPath.row] title];
        return cell;
    }else{
        static NSString *identifier1 = @"cell1";
        LZHistoryTableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:identifier1];
        if (!cell1) {
            cell1 = [[LZHistoryTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier1];
        }
        self.navigationItem.rightBarButtonItem.title = @"";
        cell1.selectionStyle = 0;
        _tableView.tableHeaderView = _clearHistoryButton;
        cell1.dic = _sortArray[indexPath.row];
        return cell1;
    }
}
#pragma mark - cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_index == 100) {
        return 44 * FITHeight;
    }
    return 100 * FITHeight;
}
#pragma mark - cell点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_flag == 0) { //降序
        self.block(indexPath.row);
        [[NSUserDefaults standardUserDefaults] setInteger:1 forKey:@"cell0"];
//        [[LZMusicSingleton shareInstance] play:_dic];
    }else{//升序
        self.block(_dataArray.count - 1 - indexPath.row);
        [[NSUserDefaults standardUserDefaults] setInteger:1 forKey:@"cell0"];
//        [[LZMusicSingleton shareInstance] play:_dic];
    }
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
